﻿using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace RibbonCustomization.RibbonWebParts.SimpleRibbonPart
{
    [ToolboxItemAttribute(false)]
    public class SimpleRibbonPart 
        : WebPart, IWebPartPageComponentProvider
    {
        WebPartContextualInfo IWebPartPageComponentProvider.WebPartContextualInfo
        {
            get 
            {
                WebPartContextualInfo info = new WebPartContextualInfo();
                info.ContextualGroups.Add(new WebPartRibbonContextualGroup
                {
                    Id = "Ribbon.MyContextualGroup",
                    VisibilityContext = "SimpleRibbonPart",
                    Command = "Ribbon.MyContextualGroup"                   
                });
                info.Tabs.Add(new WebPartRibbonTab
                {
                    Id = "Ribbon.MyContextualGroup.WebPartContextualTab",
                    VisibilityContext = "SimpleRibbonPart"
                });
                
                info.PageComponentId = SPRibbon.GetWebPartPageComponentId(this);
                info.InitPageComponentScript = 
                    String.Format(
                        @"var wp = new RibbonCustomization.SampleWebPartComponent('{0}');
                        SP.Ribbon.PageManager.get_instance().addPageComponent(wp);",
                        SPRibbon.GetWebPartPageComponentId(this));
                return info;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            ScriptLink.RegisterScriptAfterUI(Page, 
                "RibbonCustomization/SampleWebPartComponent.js", false, true);            
            base.OnPreRender(e);
        }
    }
}
